<!--
 * @Author: anshengtyp
 * @Date: 2023-12-13 10:54:22
 * @LastEditTime: 2024-01-29 17:21:22
 * @LastEditors: anshengtyp
 * @Descriptio： 部门已经完成任务统计
-->
<template>
  <div id="deptTaskComplete">

  </div>
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons')
import resize from '../dashboard/mixins/resize';
import { getCompletedTasks } from "@/api/indexCount.js";

export default {
  mixins: [resize],
  props: ['dept'],
  data() {
    return {
      chart: null,
      currentYear: new Date().getFullYear(),
      dataList: [],
      totalTask: 0
    };
  },

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
  },
  watch: {
    dept: {
      async handler(val) {
        const res = await getCompletedTasks(this.currentYear);
        for (const key in res.data) {
          if (Object.hasOwn(res.data, key)) {
            const value = res.data[key];
            let target = val.find(item => item.deptId === key);
            if (target) {
              this.totalTask += (+value);
              this.dataList.push({
                name: target.deptName,
                value: +value
              });
            }
          }
        }
        this.initChart();
      },
    }
  },

  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.chart.setOption({
        title:
          [{
            text: `${this.currentYear}年度部门已完成任务统计`,
          },
          {
            text: `完成任务总数`,
            top: '50%',
            left: '42%',
            textStyle: {
              color: '#999'
            },
          },
          {
            text: `${this.totalTask}个`,
            top: '55%',
            left: 'center',
            padding: [15, 0, 0, -20],
            textStyle: {
              fontSize: 30,
            },
          }],
        tooltip: {
          trigger: 'item',
          formatter: (params) => {
            return params.data.name +
              `<br/><span style="display:inline-block;width:10px;height:10px;background-color:${params.color};border-radius:50%;"></span> ${this.currentYear}年度完成任务数量：`
              + params.data.value
          }
        },
        legend: {
          align: 'left',
          orient: 'vertical',
          icon: 'circle',
          left: 'right',
          y: 'center',
        },
        color: ['#82d7ad', '#658cee', '#687694', '#e7bb46', '#67C23A', '#F56C6C', '#409EFF'],
        series: [
          {
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['50%', '60%'],
            avoidLabelOverlap: false,
            label: {
              show: true,
              position: 'inside',
              textStyle: {
                color: '#fff', // 标签字体颜色
              },
              formatter: (value) => {
                return value.percent === undefined ? 0 + '%' : value.percent + '%';
              },
            },
            labelLine: {
              show: false
            },
            data: this.dataList
          }
        ]
      })
    }
  },
};
</script>

<style lang="scss" scoped>
#deptTaskComplete {
  width: 100%;
  height: 100%;
}
</style>
